﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WebApplication1
{
    public partial class NouvelleRecette2 : System.Web.UI.Page
    {

        protected const string connectionString = @"Provider=SQLOLEDB;server=forma1614;Database=magicrecettes;User ID=magicrecettes;Password=cook;Trusted_Connection=False";
        protected const string query = "SELECT * FROM Ingredient";

        SqlConnection con;
        //Here we declare the parameter which we have to use in our application
        SqlCommand cmd = new SqlCommand();
        SqlParameter sp1 = new SqlParameter();
        SqlParameter sp2 = new SqlParameter();
        SqlParameter sp3 = new SqlParameter();


        [Serializable]
        public class IngredientsChoisis
        {
            public string name { get; set; }
            public int quantity { get; set; }
            public int id_ingredient { get; set; }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string nom_Recette = (String)Session["Nom_Recette"];
                string query2 = "SELECT * FROM Recette WHERE nom='" + nom_Recette+ "'";
                OleDbDataAdapter da2 = new OleDbDataAdapter(query2, connectionString);
                da2.TableMappings.Add("Table", "Recette");

                DataSet ds2 = new DataSet();
                da2.Fill(ds2);

                for (int i = 0; i < ds2.Tables["Recette"].Rows.Count; i++)
                {
                    if (ds2.Tables["Recette"].Rows[i]["Nom"].ToString() == nom_Recette)
                    {
                        lblTest.Text = Convert.ToString(ds2.Tables["Recette"].Rows[i]["id_Recette"]);
                    }
                }

                lblNomRecette.Text =nom_Recette;
                OleDbDataAdapter da = new OleDbDataAdapter(query, connectionString);
                da.TableMappings.Add("Table", "Ingredient");
                DataSet ds = new DataSet();
                da.Fill(ds);

                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                //Création d'une Liste et ajout dans la ViewState
                List<IngredientsChoisis> list = new List<IngredientsChoisis>();
                ViewState.Add("ListeIngredients", list);

                //----------------------------------------------------
                for (int i = 0; i < ds.Tables["Ingredient"].Rows.Count; i++)
                {

                    lstIngredients.Items.Add(ds.Tables["Ingredient"].Rows[i]["Nom"].ToString());

                }

            }
        }

        protected void lstIngredients_SelectedIndexChanged(object sender, EventArgs e)
        {
            OleDbDataAdapter da = new OleDbDataAdapter(query, connectionString);
            da.TableMappings.Add("Table", "Ingredient");
            DataSet ds = new DataSet();
            da.Fill(ds);

            for (int i = 0; i < ds.Tables["Ingredient"].Rows.Count; i++)
            {
                if (ds.Tables["Ingredient"].Rows[i]["Nom"].ToString() == lstIngredients.SelectedValue)
                {
                    lblUniteIngredient.Text = ds.Tables["Ingredient"].Rows[i]["Unite"].ToString();
                    lstIngredients.SelectedItem.Value = ds.Tables["Ingredient"].Rows[i]["IdIngredient"].ToString();
                   
                }
            } 

        }

        protected void lstIngredientsChoisis_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void btnChoix_Click(object sender, EventArgs e)
        {

            lstIngredientsChoisis.Items.Add(lstIngredients.SelectedItem.Text + " : " + txtQuantite.Text + " " + lblUniteIngredient.Text);
            List<IngredientsChoisis> list = (List<IngredientsChoisis>)ViewState["ListeIngredients"];
            var ic = new IngredientsChoisis(); //ic = ingrédients choisis
            ic.quantity = Int32.Parse(txtQuantite.Text);  //Quantité 
            ic.name = lstIngredients.SelectedItem.Text; //Nom de l'ingredient
            ic.id_ingredient = Int32.Parse(lstIngredients.SelectedItem.Value); //IdIngredient
            list.Add(ic);

            ViewState.Add("ListIngredients", list);

            txtQuantite.Text = "";
            lblUniteIngredient.Text = "";
            lstIngredients.SelectedItem.Selected = false;
        }

        protected void btnCreerRecette_Click(object sender, EventArgs e)
        {
            List<IngredientsChoisis> list = (List<IngredientsChoisis>)ViewState["ListeIngredients"];
            con = new SqlConnection("server=FORMA1614;Database=magicrecettes;User ID=magicrecettes;Password=cook;Trusted_Connection=False");

            cmd = new SqlCommand("dbo.CreateComposition", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();


            //string s = "";
           

           // lblTest.Text = s;

            try
            {
                foreach (IngredientsChoisis item in list)
                {
                    //Enregistrement Composition
                    cmd.Parameters.Add("@Recette_id", SqlDbType.Int).Value = Int32.Parse(lblTest.Text); //Id de la recette dans la table recette
                    cmd.Parameters.Add("@Ingredient_id", SqlDbType.Int).Value = item.id_ingredient;
                    cmd.Parameters.Add("@Quantite", SqlDbType.Int).Value = item.quantity;

                    cmd.ExecuteNonQuery();
                    //  s += item.quantity + item.name;
                    cmd.Parameters.Clear();
                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }


            lstIngredientsChoisis.Items.Clear();
            MessageBox.Show("Recette ajoutée.");

            Response.Redirect("~/NouvelleRecette.aspx", true);
        }

    }
}